| Conditions | 17 |
| Paths | 6 |
| Total Lines | 64 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like module.exports often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | const ApiError = require('../util/api_error') |
||
| 9 | rules[field].split('|').forEach(rule => { |
||
| 10 | // 普通的message key名称 |
||
| 11 | msgKey = field + '.' + rule |
||
| 12 | |||
| 13 | if (rule === 'required') { |
||
| 14 | if (!!input[field] === false) { |
||
| 15 | let msg = !!message[msgKey] === true ? message[msgKey] : field + ' 不能为空' |
||
| 16 | throw new ApiError('validate.error', msg) |
||
| 17 | } |
||
| 18 | return true |
||
| 19 | } |
||
| 20 | |||
| 21 | // 非必填项,有值再进行判断 |
||
| 22 | if (!!input[field] === false) return true |
||
| 23 | |||
| 24 | // 特殊条件 |
||
| 25 | if (rule.indexOf(':') > 0) { |
||
| 26 | let checkRule = rule.split(':') |
||
| 27 | msgKey = field + '.' + checkRule[0] |
||
| 28 | switch (checkRule[0]) { |
||
| 29 | case 'in': |
||
| 30 | let ruleData = checkRule[1].split(',') |
||
| 31 | // 数组 |
||
| 32 | if (ruleData instanceof Array === true) { |
||
| 33 | if (ruleData.indexOf(input[field]) === -1) { |
||
| 34 | let msg = !!message[msgKey] === true ? message[msgKey] : field + ' 非法' |
||
| 35 | throw new ApiError('validate.error', msg) |
||
| 36 | } |
||
| 37 | } else { |
||
| 38 | let msg = '规则设置错误' |
||
| 39 | throw new ApiError('validate.error', msg) |
||
| 40 | } |
||
| 41 | break |
||
| 42 | case 'min': |
||
| 43 | // todo |
||
| 44 | break |
||
| 45 | case 'max': |
||
| 46 | // todo |
||
| 47 | break |
||
| 48 | default: |
||
| 49 | console.log('error “:” rule: ' + rule) |
||
| 50 | let msg = '参数验证详细规则错误' |
||
| 51 | throw new ApiError('validate.error', msg) |
||
| 52 | break |
||
| 53 | } |
||
| 54 | return true |
||
| 55 | } |
||
| 56 | |||
| 57 | // 简单条件 |
||
| 58 | switch (rule) { |
||
| 59 | case 'numeric': |
||
| 60 | if (isNaN(input[field]) === true) { |
||
| 61 | let msg = !!message[msgKey] === true ? message[msgKey] : field + ' 必须为数字' |
||
| 62 | throw new ApiError('validate.error', msg) |
||
| 63 | } |
||
| 64 | break |
||
| 65 | default: |
||
| 66 | console.log('error rule: ' + rule) |
||
| 67 | let msg = '参数验证规则错误' |
||
| 68 | throw new ApiError('validate.error', msg) |
||
| 69 | break |
||
| 70 | } |
||
| 71 | return true |
||
| 72 | }) |
||
| 73 | |||
| 76 | } |